const { showLoading, hideLoading } = require('../../utils/util')
const _ = require('../../services/checklist')
import { logout } from '../../services/user'
const app = getApp()

Page({
  data: {
    today: {
      id: "0",
      icon: 'today',
      title: '我的一天',
      unfinished: 0
    },
    checkedCid: "0",
    checklists: [],
    addChecklistModalVisible: false
  },

  onLoad: function () {
    if (app.state.checklists) {
      let todayUnfinished = 0
      const checklists = app.state.checklists.map((u) => {
        const { tasks, ...checklist } = u
        const unfinishedTasks = tasks.filter((v) => !v.isDone)
        todayUnfinished += unfinishedTasks.filter((v) => v.today).length
        checklist['unfinished'] = unfinishedTasks.length
        return checklist
      })

      this.setData({
        checklists,
        'today.unfinished': todayUnfinished
      })
    }

    this.setData({
      checkedCid: app.state.cid
    })
  },

  showAddNewChecklistModal: function () {
    console.log('show')
    this.setData({
      'addChecklistModalVisible': true
    })
  },

  handleSearch: function (e) {
    console.log('search!', e)
  },
  handleChecklistClicked: function ({ target: { dataset: { cid } } }) {
    app.state.cid = cid
    wx.navigateBack()
  },
  handleClickUserinfo: function (e) {
    wx.showActionSheet({
      itemList: ['注销'],
      success: function ({ tapIndex }) {
        if (tapIndex === 0) {
          logout()
          app.state.userInfo = {}
          app.state.checklists = []
          app.state.cid = "0"
        }
      }
    })
  },
  addNewChecklist: function ({ detail: title }) {
    const _this = this
    showLoading()
    const newList = {
      title,
      icon: 'list',
      userId: app.state.userInfo.id
    }
    _.addNewList(newList).then((data) => {
      hideLoading()
      app.addNewChecklist(data)
      this.setData({
        addChecklistModalVisible: false
      })
      app.state.cid = data.id
      wx.navigateBack()
    }).catch(() => {
      hideLoading()
    })
  },
  deleteChecklist: function ({ detail: { id: listId, title } }) {
    const _this = this
    wx.showModal({
      title: '你确定吗？',
      content: `"${title}"将被永远删除`,
      success: function (res) {
        if (res.confirm) {
          showLoading()
          _.deleteChecklist(listId).then((data) => {
            hideLoading()
            app.deleteChecklist(listId)
            if (app.state.cid === listId) {
              // 如果删除的清单是当前选择的清单 则讲选择的清单只想我的一天
              app.state.cid = "0"
              _this.setData({
                checklists: _this.data.checklists.filter((u) => u.id !== listId)
              })
            } else {
              _this.setData({
                checklists: _this.data.checklists.filter((u) => u.id !== listId)
              })
            }
            wx.showToast({
              title: '删除成功',
              icon: 'success'
            })
          }).catch(({ code }) => {
            hideLoading()
            if (code && code === 4005) {
              wx.showToast({
                title: '清单不存在',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  }
})
